const express = require('express')
const router = express.Router()

// 自定义库
const upload = require('../config/upload.js')
const db = require('../config/database.js')
const code = require('../config/code.js')
const File = require('../model/file.js')

router.post('/upload', upload.single('pptimg'), (req, res, next) => {
  (async () => {
    const [errAdd, results] = await db.query(File.addFile, [req.auth.id, ...Object.values(req.file)])
    if (errAdd) {
      res.json({code: code.dbErr, msg: '数据库错误'})
      return
    }
    if (results.affectedRows === 0) {
      res.json({code: code.dbErr, msg: '数据库错误'})
      return      
    }
    res.json({
      code: code.ok,
      msg: '存储成功',
      data: {
        id: results.insertId,
        ...req.file
      }
    })
  })()
  return
})


module.exports = router
